package com.pdx.controller;

import com.baidu.aip.face.AipFace;
import com.pdx.service.FaceLoginService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author PDX
 * @website https://blog.csdn.net/Gaowumao
 * @Date 2022-05-08 18:04
 * @Description
 */
@Controller
@RequestMapping("/face")
public class FaceLoginController {

    @Autowired
    private FaceLoginService faceLoginService;

    @RequestMapping("/")
    public String toLogin(){
        return "index";
    }
    //人脸登录
    @RequestMapping("/toFace")
    public String toFace(){
        return "getface.html";
    }
    //人脸注册
    @RequestMapping("/toRegisterFace")
    public String toRegisterFace(){
        return "register-face.html";
    }

//    /**
//     * 人脸登录
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/face-login")
//    @ResponseBody
//    public  String searchface(@RequestBody @RequestParam(name = "imagebast64") StringBuffer imagebast64, HttpServletRequest request) throws Exception {
//        String userId = faceLoginService.loginByFace(imagebast64);
//        request.getSession().setAttribute("userId",userId);
//        request.getSession().setAttribute("username","派大星");
//        return userId;
//    }
//
//    /**
//     * 人脸登录
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/face-register")
//    @ResponseBody
//    public  String registerFace(@RequestBody @RequestParam(name = "imagebast64") StringBuffer imagebast64, HttpServletRequest request) throws Exception {
//        String str = faceLoginService.registerFace("1001",imagebast64);
//        request.getSession().setAttribute("RuserId","1001");
//        request.getSession().setAttribute("Rusername","派大星的朋友");
//        return str;
//    }

    /**
     * 人脸登录
     */
    @RequestMapping("/face-login")
    @ResponseBody
    public  String searchface(@RequestBody @RequestParam(name = "imagebast64") StringBuffer imagebast64, HttpServletRequest request) throws Exception {
        /**
         * 创建一个链接对象
         */
        AipFace client = new AipFace("26219844","SCH5gGu117Zh37IsYe8d4OgP","txCMz9AjnLpcokwxWEnkZC0unWj81R29");
        /**
         * 人脸搜索
         */
        String image = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());
        JSONObject res = client.search(image, "BASE64", "pdx", null);
        if (res.has("error_code") && res.getInt("error_code") == 0){
            JSONObject result = res.getJSONObject("result");
            JSONArray userList = result.getJSONArray("user_list");
            if (userList.length() > 0){
                JSONObject user = userList.getJSONObject(0);
                double score = user.getDouble("score");
                if (score > 80){
                    System.out.println(user.getString("user_id"));
                    return user.getString("user_id");
                }
            }
        }
        return null;

    }

    /**
     * 人脸注册
     */
    @RequestMapping("/face-register")
    @ResponseBody
    public  String registerFace(@RequestBody @RequestParam(name = "imagebast64") StringBuffer imagebast64, HttpServletRequest request) throws Exception {
        /**
         * 创建一个链接对象
         */
        AipFace client = new AipFace("26219844","SCH5gGu117Zh37IsYe8d4OgP","txCMz9AjnLpcokwxWEnkZC0unWj81R29");
        /**
         * 人脸搜索
         */
        HashMap<String,String> map = new HashMap<>();
        map.put("quality_control","NORMAL");//图片质量
        map.put("liveness_control","LOW");//活体检测
        String image = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());
        JSONObject res = client.addUser(image, "BASE64", "pdx", "10001",map);
        Integer errorCode = res.getInt("error_code");
        if (errorCode == 0? true : false){
            System.err.println("注册成功");
        }else {
            System.out.println("注册失败");
        }
        return res.toString();
    }

    @RequestMapping("/success")
    public String success(){
        return "success";
    }

    @RequestMapping("/resuccess")
    public String Rsuccess(){
        return "R-success";
    }

}
